package com.realbrand.net.realbrandpay.ui.activity.wallet.set;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.SpannedString;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.realbrand.net.realbrandpay.R;
import com.realbrand.net.realbrandpay.api.HTTPAPI;
import com.realbrand.net.realbrandpay.base.BaseActivity;
import com.realbrand.net.realbrandpay.inter.IPayPassword;
import com.realbrand.net.realbrandpay.inter.IVerifyOldPasswd;
import com.realbrand.net.realbrandpay.untils.Static;
import com.realbrand.net.realbrandpay.zxing.util.L;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * Created by Administrator on 2017/9/11.
 */

public class ChangePayPasswdActivity extends BaseActivity {
    @BindView(R.id.changeLoginPasswdActivity_toolbar)
    Toolbar changeLoginPasswdActivityToolbar;
    @BindView(R.id.changeLoginPasswdActivity_et_oldPasswd)
    EditText changeLoginPasswdActivityEtOldPasswd;
    @BindView(R.id.changeLoginPasswdActivity_tv_toPhone)
    TextView changeLoginPasswdActivityTvToPhone;
    @BindView(R.id.changeLoginPasswdActivity_bt_next)
    Button changeLoginPasswdActivityBtNext;

    //用户名
    private String UserName;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        ButterKnife.bind(this);
        changeLoginPasswdActivityToolbar.setTitle("");
        changeLoginPasswdActivityToolbar.setNavigationIcon(R.mipmap.ic_back);
        changeLoginPasswdActivityToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(ChangePayPasswdActivity.this, PasswordManageActivity.class));
                finish();
            }
        });
        setOnClick();
        //获取用户名
        SharedPreferences massage = getSharedPreferences("userMassage", MODE_PRIVATE);
        UserName = massage.getString("account", "");
        SpannableString ss = new SpannableString("请输入旧密码");//定义hint的值
        AbsoluteSizeSpan ass = new AbsoluteSizeSpan(16, true);//设置字体大小 true表示单位是sp
        ss.setSpan(ass, 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        changeLoginPasswdActivityEtOldPasswd.setHint(new SpannedString(ss));
    }

    @Override
    protected int getLayoutId() {
        return R.layout.changepaypasswd_activity;
    }

    @Override
    protected void setOnClick() {
        changeLoginPasswdActivityBtNext.setOnClickListener(this);
        changeLoginPasswdActivityTvToPhone.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.changeLoginPasswdActivity_bt_next:
                //先判断用户有没有输入密码
                String passwdByUser = changeLoginPasswdActivityEtOldPasswd.getText().toString();
                if (TextUtils.isEmpty(passwdByUser)) {
                    Static.toast(this, "请输入旧密码");
                } else {
                    //验证用户输入的密码是否正确
                    verifyOldPasswd(passwdByUser);
                }
                break;
            //转换到手机验证修改
            case R.id.changeLoginPasswdActivity_tv_toPhone:
                startActivity(new Intent(this, ChangePayPasswdVerifyActivity.class));
                finish();
                break;
        }
    }

    /**
     * 判断旧密码是否正确
     *
     * @param passwdByUser
     */
    private void verifyOldPasswd(String passwdByUser) {
        JSONObject object = new JSONObject();
        try {
            object.put("SOURCE", "ChangePayPasswdActivity");
            object.put("CREDENTIALS", "0");
            object.put("TERMINAL", "1");
            object.put("INDEX", Static.getDay());
            object.put("METHOD", "PayPasswdProving");
            object.put("UserAccount", UserName);
            object.put("PayPasswd", passwdByUser);
            RequestBody body = RequestBody.create(HTTPAPI.JSON, object.toString());
            Retrofit retrofit = new Retrofit.Builder().baseUrl(HTTPAPI.BASE_URL).build();
            IPayPassword iVerifyOldPasswd = retrofit.create(IPayPassword.class);
            Call<ResponseBody> verify = iVerifyOldPasswd.verify(body);
            verify.enqueue(new Callback<ResponseBody>() {
                @Override
                public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                    if (response == null) {
                        Static.toast(ChangePayPasswdActivity.this, "网络繁忙。。。");
                    } else {
                        try {
                            String result = response.body().string();
                            L.d(L.TAG, "验证旧密码的结果" + result);
                            try {
                                JSONObject json = new JSONObject(result);
                                JSONArray data = json.getJSONArray("DATA");
                                JSONObject jsonObject = data.getJSONObject(0);
                                String dataString = jsonObject.getString("result");
                                if ("true".equals(dataString)) {
                                    //表示旧密码正确，跳转到输入新密码界面
                                    startActivity(new Intent(ChangePayPasswdActivity.this, PayPasswdChangeActivity.class));
                                    finish();
                                } else if ("密码输入错误".equals(dataString)) {
                                    Static.toast(ChangePayPasswdActivity.this, "密码错误");
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }

                @Override
                public void onFailure(Call<ResponseBody> call, Throwable t) {

                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
